package service

import (
	__ "activity_srv/basic/proto"
	"activity_srv/handler/dao"
	"context"
	"errors"
)

// SetFaceId 设置用户面容ID信息
func (s *Serve) SetFaceId(_ context.Context, in *__.SetFaceIdReq) (*__.SetFaceIdResp, error) {
	// 检查参数
	if in.UserId <= 0 || in.FaceToken == "" || in.FaceData == "" {
		return &__.SetFaceIdResp{
			Success: false,
			Message: "参数错误",
		}, nil
	}

	// 检查状态值是否合法
	if in.Status != 1 && in.Status != 2 {
		return &__.SetFaceIdResp{
			Success: false,
			Message: "状态值不合法，只能是1(启用)或2(禁用)",
		}, nil
	}

	// 调用DAO层设置面容ID
	if err := dao.SetFaceId(in); err != nil {
		return &__.SetFaceIdResp{
			Success: false,
			Message: "设置面容ID失败: " + err.Error(),
		}, nil
	}

	return &__.SetFaceIdResp{
		Success: true,
		Message: "设置面容ID成功",
	}, nil
}

// GetFaceId 获取用户面容ID信息
func (s *Serve) GetFaceId(_ context.Context, in *__.GetFaceIdReq) (*__.GetFaceIdResp, error) {
	// 检查参数
	if in.UserId <= 0 {
		return nil, errors.New("用户ID不能为空")
	}

	// 调用DAO层获取面容ID
	faceId, err := dao.GetFaceId(in)
	if err != nil {
		return nil, err
	}

	// 构造响应
	resp := &__.GetFaceIdResp{
		UserId:    int64(faceId.UserId),
		FaceToken: faceId.FaceToken,
		Status:    int32(faceId.Status),
		CreatedAt: faceId.CreatedAt.Format("2006-01-02 15:04:05"),
		UpdatedAt: faceId.UpdatedAt.Format("2006-01-02 15:04:05"),
	}

	return resp, nil
}

// UpdateFaceIdStatus 更新面容ID状态
func (s *Serve) UpdateFaceIdStatus(_ context.Context, in *__.UpdateFaceIdStatusReq) (*__.UpdateFaceIdStatusResp, error) {
	// 检查参数
	if in.UserId <= 0 {
		return &__.UpdateFaceIdStatusResp{
			Success: false,
			Message: "用户ID不能为空",
		}, nil
	}

	// 检查状态值是否合法
	if in.Status != 1 && in.Status != 2 {
		return &__.UpdateFaceIdStatusResp{
			Success: false,
			Message: "状态值不合法，只能是1(启用)或2(禁用)",
		}, nil
	}

	// 调用DAO层更新面容ID状态
	if err := dao.UpdateFaceIdStatus(in); err != nil {
		return &__.UpdateFaceIdStatusResp{
			Success: false,
			Message: "更新面容ID状态失败: " + err.Error(),
		}, nil
	}

	statusText := "禁用"
	if in.Status == 1 {
		statusText = "启用"
	}

	return &__.UpdateFaceIdStatusResp{
		Success: true,
		Message: "面容ID已" + statusText,
	}, nil
}

// DeleteFaceId 删除用户面容ID信息
func (s *Serve) DeleteFaceId(_ context.Context, in *__.DeleteFaceIdReq) (*__.DeleteFaceIdResp, error) {
	// 检查参数
	if in.UserId <= 0 {
		return &__.DeleteFaceIdResp{
			Success: false,
			Message: "用户ID不能为空",
		}, nil
	}

	// 调用DAO层删除面容ID
	if err := dao.DeleteFaceId(in); err != nil {
		return &__.DeleteFaceIdResp{
			Success: false,
			Message: "删除面容ID失败: " + err.Error(),
		}, nil
	}

	return &__.DeleteFaceIdResp{
		Success: true,
		Message: "删除面容ID成功",
	}, nil
}

// VerifyFaceId 验证面容ID
func (s *Serve) VerifyFaceId(_ context.Context, in *__.VerifyFaceIdReq) (*__.VerifyFaceIdResp, error) {
	// 检查参数
	if in.UserId <= 0 || in.FaceToken == "" {
		return &__.VerifyFaceIdResp{
			Success: false,
			Message: "参数错误",
		}, nil
	}

	// 调用DAO层验证面容ID
	verified, err := dao.VerifyFaceId(in)
	if err != nil {
		return &__.VerifyFaceIdResp{
			Success: false,
			Message: err.Error(),
		}, nil
	}

	if !verified {
		return &__.VerifyFaceIdResp{
			Success: false,
			Message: "面容验证失败",
		}, nil
	}

	return &__.VerifyFaceIdResp{
		Success: true,
		Message: "面容验证成功",
	}, nil
}
